<template>
  <!-- 审核意见 -->
  <div>
    <FormPanel ref="formPanel" :list="initForm" :cols="1" :formType="pdata.infoBasic" :labelWidth="130" :isSubmitBtn="false" @formChange="formChangeHandle" class="mtop" />
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import autoLoding from '@/utils/loding'; //等待框

import { saveReview } from '@/api/cms-api/customerManage/approvalManage/review';
import * as apiType from '@/api/cms-api/customerManage/approvalManage/types';

export default {
  name: 'approvalDetailsInfoIdea',
  mixins: [dictionary, autoLoding],
  model: {
    prop: 'pdata',
    event: 'click'
  },
  props: {
    pdata: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      formValue: {},
      isShow: this.pdata.infoBasic === 'view',
      isDep: apiType.PROJECT_STATUS_REVIEW === this.pdata.examineType,
      isMinister: apiType.PROJECT_STATUS_DEP_REVIEW === this.pdata.examineType,
      //大客户部已审核
      isChiefInspector: apiType.PROJECT_STATUS_MINISTER_REVIEW === this.pdata.examineType,
      initForm: []
    };
  },
  created() {
    // console.log(this.pdata.formData['data_' + apiType.PROJECT_STATUS_REVIEW].checkDesc);
    this.initForm = this.initFormFun();
    // console.log(this.isShow,!this.isDep,this.pdata.infoBasic);
  },
  methods: {
    initFormData() {
      this.initForm.forEach(x => {
        x.initialValue = (this.pdata.formData.allForm[x.dataType] || {}).checkDesc || '';
      });
    },
    //审核表单
    initFormFun() {
      return [
        // {
        //   type: 'TEXT_AREA',
        //   label: '事业部审核意见',
        //   fieldName: 'depCheckDesc',
        //   dataType: 'data_' + apiType.PROJECT_STATUS_DEP_REVIEW,
        //   disabled: this.isShow || !this.isDep,
        //   rows: 2,
        //   initialValue: '',
        //   placeholder: '',
        //   rules: [],
        //   rulesTmp: [
        //     { required: true, message: '请输入事业部审核意见', trigger: 'blur' },
        //     { min: 0, max: 500, message: '长度在 0 到 500 个字符', trigger: 'blur' }
        //   ]
        // },
        // {
        //   type: 'TEXT_AREA',
        //   label: '事业部部长审核意见',
        //   fieldName: 'ministerCheckDesc',
        //   dataType: 'data_' + apiType.PROJECT_STATUS_MINISTER_REVIEW,
        //   rows: 2,
        //   disabled: this.isShow || !this.isMinister,
        //   initialValue: '',
        //   placeholder: '',
        //   rules: [],
        //   rulesTmp: [
        //     { required: true, message: '请输入事业部部长审核意见', trigger: 'blur' },
        //     { min: 0, max: 500, message: '长度在 0 到 500 个字符', trigger: 'blur' }
        //   ]
        // },
        {
          type: 'TEXT_AREA',
          label: '审核意见',
          fieldName: 'chiefInspectorCheckDesc',
          dataType: 'data_' + apiType.PROJECT_STATUS_CHIEF_INSPECTOR_REVIEW,
          disabled: this.isShow || !this.isChiefInspector,
          rows: 2,
          initialValue: '',
          placeholder: '',
          rules: [],
          rulesTmp: [
            { required: true, message: '请输入审核意见', trigger: 'blur' },
            { min: 0, max: 500, message: '长度在 0 到 500 个字符', trigger: 'blur' }
          ]
        }
        // {
        // 	type: 'UPLOAD_FILE',
        // 	label: '附件',
        // 	fieldName: 'uploadFileList',
        // 	disabled: this.isShow || (!this.isDep && !this.isMinister && !this.isChiefInspector),
        // 	upload: {
        // 		actionUrl: '/api/file/oss/upload', //上传的地址
        // 		// fixedSize: [300, 500], //剪裁尺寸
        // 		// isCalcHeight: false,
        // 		limit: 1 //上传数量
        // 	},
        // 	initialValue: '',
        // 	placeholder:'',
        // }
      ];
    },
    formChangeHandle(val) {
      this.loadingVm = this.openLoding('.cover-container');
      this.formValue = val;
      this.pdata.examineFormData = this.formValue;
      this.$emit('click', this.pdata); //自定义组件中使用v-model都需要通过$emit触发

      //处理数据
      // let data = this.ideaInfoData.examineFormData;
      let data = val;
      data.projectId = this.pdata.projectId;
      if (this.type === 1) {
        //通过
        // if (this.isDep) {
        //   //已提交
        //   data.checkType = apiType.PROJECT_STATUS_DEP_REVIEW;
        // } else if (this.isMinister) {
        //   //事业部已审核
        //   data.checkType = apiType.PROJECT_STATUS_MINISTER_REVIEW;
        // } else if (this.isChiefInspector) {
        //   //大客户部已审核
        //   data.checkType = apiType.PROJECT_STATUS_CHIEF_INSPECTOR_REVIEW;
        //   //设置为已生效()
        //   //this.formData.allForm.projectFollowUpStatus = apiType.PROJECT_FOLLOW_UP_STATUS_EFFECTIVE;
        //   data.projectFollowUpStatus = apiType.PROJECT_FOLLOW_UP_STATUS_EFFECTIVE;
        //   data.customerProvince = this.pdata.formData.allForm.customerProvince;
        // }

        if (this.isChiefInspector) {
          //大客户部已审核
          data.checkType = apiType.PROJECT_STATUS_CHIEF_INSPECTOR_REVIEW;
          //设置为已生效()
          //this.formData.allForm.projectFollowUpStatus = apiType.PROJECT_FOLLOW_UP_STATUS_EFFECTIVE;
          data.projectFollowUpStatus = apiType.PROJECT_FOLLOW_UP_STATUS_EFFECTIVE;
          data.customerProvince = this.pdata.formData.allForm.customerProvince;
        }
      } else {
        //不通过,则使用负数的状态，以表示驳回
        // if (this.isDep) {
        //   //已提交
        //   data.checkType = apiType.PROJECT_STATUS_DEP_REVIEW_NO;
        // } else if (this.isMinister) {
        //   //事业部已审核
        //   data.checkType = apiType.PROJECT_STATUS_MINISTER_REVIEW_NO;
        // } else if (this.isChiefInspector) {
        //   //大客户部已审核
        //   data.checkType = apiType.PROJECT_STATUS_CHIEF_INSPECTOR_REVIEW_NO;
        // }

        if (this.isChiefInspector) {
          //大客户部已审核
          data.checkType = apiType.PROJECT_STATUS_CHIEF_INSPECTOR_REVIEW_NO;
        }
      }
      //需要判断当前的审核类型是哪个,取出不同的审核意见
      // if (this.isDep) {
      //   data.checkDesc = data.depCheckDesc;
      // } else if (this.isMinister) {
      //   data.checkDesc = data.ministerCheckDesc;
      // } else if (this.isChiefInspector) {
      //   data.checkDesc = data.chiefInspectorCheckDesc;
      // }

      if (this.isChiefInspector) {
        data.checkDesc = data.chiefInspectorCheckDesc;
      }
      this.$nextTick(() => this.saveExamine(data));
    },
    //保存
    saveDialog(type) {
      this.type = type;
      if (this.type === 1) {
        //通过
        // if (this.isDep) {
        //   this.initForm.find(item => item.fieldName === 'depCheckDesc').rules = null;
        // } else if (this.isMinister) {
        //   this.initForm.find(item => item.fieldName === 'ministerCheckDesc').rules = null;
        // } else if (this.isChiefInspector) {
        //   this.initForm.find(item => item.fieldName === 'chiefInspectorCheckDesc').rules = null;
        // }

        if (this.isChiefInspector) {
          this.initForm.find(item => item.fieldName === 'chiefInspectorCheckDesc').rules = null;
        }
      } else if (this.type === -1) {
        //驳回
        // if (this.isDep) {
        //   let depCheckDesc = this.initForm.find(item => item.fieldName === 'depCheckDesc');
        //   depCheckDesc.rules = depCheckDesc.rulesTmp;
        // } else if (this.isMinister) {
        //   let ministerCheckDesc = this.initForm.find(item => item.fieldName === 'ministerCheckDesc');
        //   ministerCheckDesc.rules = ministerCheckDesc.rulesTmp;
        // } else if (this.isChiefInspector) {
        //   let chiefInspectorCheckDesc = this.initForm.find(item => item.fieldName === 'chiefInspectorCheckDesc');
        //   chiefInspectorCheckDesc.rules = chiefInspectorCheckDesc.rulesTmp;
        // }

        if (this.isChiefInspector) {
          let chiefInspectorCheckDesc = this.initForm.find(item => item.fieldName === 'chiefInspectorCheckDesc');
          chiefInspectorCheckDesc.rules = chiefInspectorCheckDesc.rulesTmp;
        }
      }
      //刷新验证内容（框架升级，不在需要手动处理验证信息）
      // this.$refs.formPanel.rules = this.$refs.formPanel.createFormRule(this.initForm);
      // this.$nextTick(() => this.$refs.formPanel.$refs.form.clearValidate());

      this.$nextTick(() => this.$refs.formPanel.SUBMIT_FORM());
    },
    async saveExamine(data) {
      await saveReview(data).then(res => {
        if (res.resultCode === 200) {
          this.closeLoding(this.loadingVm);
          this.$notify.success({ title: '提示信息', message: '审核成功' });
          this.$emit('close', 'refresh');
        } else {
          this.closeLoding(this.loadingVm);
          this.$notify.error({ title: '提示信息', message: res.errMsg || '提交失败!' });
        }
      });
    }
  }
};
</script>
